<!DOCTYPE html>
<html>
<head>
    <title>任务新增</title>
    {% include 'system/common/header.html' %}</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">ID标识</label>
                <div class="layui-input-block">
                    <input type="text" name="id" lay-verify="title" autocomplete="off" placeholder="请输入任务id"
                            class="layui-input" value="{{ job.id | safe }}" required>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入名称"
                            class="layui-input" value="{{ job.name | safe }}" required>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">任务类型</label>
                <div class="layui-input-block">
                    <select name="type" lay-filter="type" lay-verify="required" lay-search>
                        <option value="date" {% if job.trigger == "date" %} selected {% endif %}>date 一次性指定固定时间，只执行一次</option>
                        <option value="interval" {% if job.trigger == "interval" %} selected {% endif %}>interval 间隔调度，隔多长时间执行一次</option>
                        <option value="cron" {% if job.trigger == "cron" %} selected {% endif %}>cron 指定相对时间执行</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">函数</label>
                <div class="layui-input-block">
                    <select name="functions" lay-verify="required" lay-search>
                        {% for task in task_list %} 
                        <option value="{{ task }}" {% if job.functions == task %} selected {% endif %}>{{ task }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
            <div class="layui-form-item" id="datetimeItem" >
                <label class="layui-form-label">固定时间</label>
                <div class="layui-input-block">
                    <input name="datetime" type="datetime" class="layui-input" id="datetime">
                </div>
            </div>
            <div class="layui-form-item" id="intervalItem" style="display: none">
                <label class="layui-form-label">间隔时间</label>
                <div class="layui-input-block">
                    <input name="interval" type="text" class="layui-input" placeholder="秒、分钟、小时、天（如: 30 秒）" id="interval">
                </div>
            </div>
            <div class="layui-form-item" id="cronItem" style="display: none">
                <label class="layui-form-label">Cron 表达式</label>
                <div class="layui-input-block">
                    <input name="cron" type="text" class="layui-input" placeholder="Cron表达式: 15 10 * * 1-5 解释:每周一至周五的10:15" id="cron">
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="task-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>

    layui.use(['form', 'jquery'], function () {

        let form = layui.form
        let $ = layui.jquery
        var laydate = layui.laydate;
        let MODULE_PATH = "{{ url_for('system.adminTask.main') }}"
        form.on('select(type)', function (data) {
            // console.log(data);
            if (data.value === 'date') {
                $('#intervalItem').hide();
                $('#cronItem').hide();
                $('#datetimeItem').show();
                laydate.render({
                    elem: '#datetime'
                    , type: 'datetime'
                });
                 $('#datetime').val = '{{ job.run_date | safe }}';
            } else if (data.value === 'interval') {
                $('#datetimeItem').hide();
                $('#cronItem').hide();
                $('#intervalItem').show();
                $('#interval').val = '{{ job.run_date | safe }}';
            } else if (data.value === 'cron') {
                $('#datetimeItem').hide();
                $('#intervalItem').hide();
                $('#cronItem').show();
                $('#cron').val='{{ job.run_date | safe }}';
            }
        });
        

        form.on('submit(task-save)', function (data) {
            $.ajax({
                url: MODULE_PATH + 'save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                            parent.layui.table.reload('task-table')
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })
    })
</script>